package com.frogs.ucenter.v1.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.frogs.system.web.api.request.Request;
import com.frogs.system.web.api.response.Response;
import com.frogs.system.web.exception.ServiceOperationException;
import com.frogs.ucenter.component.VelocityMailSupportHelper;
import com.frogs.ucenter.v1.request.EmailSendRequest;
import com.frogs.ucenter.v1.response.DefaultResponse;
import com.frogs.ucenter.v1.service.EmailService;
import com.frogs.ucenter.v1.util.ApiConstants;

/**
 * 邮件现在不走这个流程，直接走webpower第三方服务，此类暂时保留
 * @author wushubin
 *
 */
@Service(ApiConstants.VERSION+"emailService")
public class EmailServiceImpl implements EmailService{

	private static final Logger log = LoggerFactory.getLogger(EmailServiceImpl.class);
	
	@Resource
	private VelocityMailSupportHelper velocityMailSupportHelper;
	
	@Override
	public Response send(Request request) throws Exception {
		Response response = new DefaultResponse();
		try{
			EmailSendRequest emailSendRequest = (EmailSendRequest) request;
			String sender = emailSendRequest.getSender();
			String content = emailSendRequest.getContent();
			String receiver = emailSendRequest.getReceiver();
			String subject = emailSendRequest.getSubject();
			String imgPath = emailSendRequest.getImgPath();
			String templateName = emailSendRequest.getTemplateName();
			String ccReceiver = emailSendRequest.getCcReceiver();
			String bccReceiver = emailSendRequest.getBccReceiver();
			String attachment = emailSendRequest.getAttachment();
			//keyValue是键值对组成的字符串，key1#;1233132132313小黑:;key2#;1234:;key3#;12345:;key4#;123456:;key5#;http://www.baidu.com:;
			Map<String,Object> map = new HashMap<String,Object>();
			if(null!=content && !"".equals(content)){
				String contentList[] = content.split(":;");
				for(int i=0;i<contentList.length;i++){
					String key = contentList[i].split("#;")[0];
					String value = contentList[i].split("#;")[1];
					map.put(key, value);
				}
			}
			velocityMailSupportHelper.setSubject(subject);
			if(null!=sender && !"".equals(sender)){
				velocityMailSupportHelper.setFrom(sender);
			}
			velocityMailSupportHelper.setTemplateName(templateName);
			String cc[] = null;
			String bcc[] = null;
			if(null!=ccReceiver && !"".equals(ccReceiver)){
				cc = ccReceiver.split(";");
			}
			if(null!=bccReceiver && !"".equals(bccReceiver)){
				bcc = bccReceiver.split(";");
			}
			
			//不通过附件直接发送图片给QQ邮箱，QQ邮箱会自动把邮件发送到垃圾邮件里面
			if(null!=imgPath && !"".equals(imgPath)&&(null==attachment || "".equals(attachment))){//正文里面发图片,但不发附件
				velocityMailSupportHelper.sendMime(receiver, cc, bcc, map, imgPath);
			}else if(null!=attachment&&!"".equals(attachment)){//通过邮件发附件同时在正文里发送图片
				velocityMailSupportHelper.sendMime(receiver, cc, bcc, map, imgPath,attachment);
			}else{//普通邮件
				velocityMailSupportHelper.sendMime(receiver, cc, bcc, map);
			}

			response.setTip("邮件发送成功!");
			return response;
		}catch (Exception e) {
			log.error("func[{}] request[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						request,Arrays.deepToString(e.getStackTrace()) });
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
	}

}
